package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.BusinessMapper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.pojo.entity.Region;
import com.heima.pojo.dto.RegionDto;
import com.heima.pojo.vo.PageVo;
import com.heima.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private BusinessMapper businessMapper;

    /**
     * 区域分页查询
     */
    @Override
    @Transactional
    public PageVo getPage(Integer pageIndex, Integer pageSize, String name) {
        //先查询区域表，把数据查询出来并进行分页
        PageHelper.startPage(pageIndex,pageSize);
        //查询区域表
        List<Region> regions= regionMapper.getRegions(name);
        Page<Region> page = (Page<Region>) regions;
        //封装
        PageVo<Region> pageVo = new PageVo<>();
        pageVo.setPageIndex(pageIndex);
        pageVo.setPageSize(pageSize);
        pageVo.setTotalCount(page.getTotal());
        pageVo.setTotalPage(page.getPages());
        pageVo.setCurrentPageRecords(regions);
        return pageVo;
    }

    /**
     * 新增区域
     * @param regionDto
     * @return
     */
    @Override
    public Boolean save(RegionDto regionDto) {
       Boolean result =  regionMapper.save(regionDto);
        return result;
    }

    /**
     * 修改区域
     * @param region
     * @return
     */
    @Override
    public Boolean update(String id,Region region) {
        Boolean result =  regionMapper.update(id,region);
        return result;
    }


    /**
     * 删除区域
     * @param id
     * @return
     */
    @Override
    public Boolean delete(Long id) {
        int i = regionMapper.deleteById(id);
        if (i==1){
            return true;
        }
        return false;
    }
}










